package com.sea.physical.service.impl;

import com.codecrane.core.web.BatchImportUtils;
import com.sea.physical.entity.Coefficient;
import com.sea.physical.entity.CoefficientSku;
import com.sea.physical.entity.Port;
import com.sea.physical.entity.StockProduce;
import com.sea.physical.mapper.CoefficientMapper;
import com.sea.physical.mapper.CoefficientSkuMapper;
import com.sea.physical.service.CoefficientSkuService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.List;

@Service
public class CoefficientSkuServiceSkuImpl implements CoefficientSkuService{
    @Autowired
    private CoefficientSkuMapper coefficientSkuMapper;

    @Override
    public List<CoefficientSku> findByCnd(CoefficientSku coefficientSku) {
        return coefficientSkuMapper.findByCnd(coefficientSku);
    }

    @Override
    public CoefficientSku findBySku(String coefficientSku, String coefficientChannelName) {
        return coefficientSkuMapper.findBySku(coefficientSku,coefficientChannelName);
    }

    @Override
    public void importSku(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<CoefficientSku> list = BatchImportUtils.iteratorForCoefficientSku(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少保存一条SKU信息！");
            } else {
                for (CoefficientSku coefficientSku : list) {
                    CoefficientSku coefSku = findBySku(coefficientSku.getCoefficientSku(), coefficientSku.getCoefficientChannelName());
                    if(null==coefSku){
                        save(coefficientSku);
                    }else{
                        updateCoefBySku(coefficientSku);
                    }
                }
            }
        }
    }

    @Override
    public Integer save(CoefficientSku coefficientSku) {
        return coefficientSkuMapper.insert(coefficientSku);
    }

    @Override
    public Integer updateCoefBySku(CoefficientSku coefficientSku) {
        return coefficientSkuMapper.updateCoefBySku(coefficientSku);
    }

    @Override
    public Integer delete(Integer id) {
        return coefficientSkuMapper.delete(id);
    }


}
